﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LetsDealData.Entities;
using LetsDeal.Data;

namespace Administrator.Controllers
{
    public class ProviderController : Controller
    {
        // private LetsDealEntities db = new LetsDealEntities();
        private IGenericRepository genericRepository;

        public ProviderController()
        {
            genericRepository = new GenericRepository(new LetsDealDbContext());
        }

        public ProviderController(IGenericRepository genericRepository)
        {
            this.genericRepository = genericRepository;
        }

        //
        // GET: /Provider/

        public ActionResult Index()
        {
            //return View(db.Providers.ToList());
            return View(genericRepository.Get<Provider>(filter: p => p.Actived == true));
        }

        //
        // GET: /Provider/Details/5

        public ActionResult Details(int id = 0)
        {
            //Provider provider = db.Providers.Find(id);
            Provider provider = genericRepository.GetById<Provider>(id);
            if (provider == null)
            {
                return HttpNotFound();
            }
            return View(provider);
        }

        //
        // GET: /Provider/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Provider/Create

        [HttpPost]
        public ActionResult Create(Provider provider)
        {
            if (ModelState.IsValid)
            {
                //db.Providers.Add(provider);
                //db.SaveChanges();
                genericRepository.Create<Provider>(provider);
                genericRepository.SaveChanged();
                return RedirectToAction("Index");
            }

            return View(provider);
        }

        //
        // GET: /Provider/Edit/5

        public ActionResult Edit(int id = 0)
        {
            //Provider provider = db.Providers.Find(id);
            Provider provider = genericRepository.GetById<Provider>(id);
            if (provider == null)
            {
                return HttpNotFound();
            }
            return View(provider);
        }

        //
        // POST: /Provider/Edit/5

        [HttpPost]
        public ActionResult Edit(Provider provider)
        {
            if (ModelState.IsValid)
            {
                //db.Entry(provider).State = EntityState.Modified;
                //db.SaveChanges();
                genericRepository.Update<Provider>(provider);
                genericRepository.SaveChanged();
                return RedirectToAction("Index");
            }
            return View(provider);
        }

        //
        // GET: /Provider/Delete/5

        public ActionResult Delete(int id = 0)
        {
            //Provider provider = db.Providers.Find(id);
            Provider provider = genericRepository.GetById<Provider>(id);
            if (provider == null)
            {
                return HttpNotFound();
            }
            return View(provider);
        }

        //
        // POST: /Provider/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            //Provider provider = db.Providers.Find(id);
            //db.Providers.Remove(provider);
            //db.SaveChanges();
            Provider provider = genericRepository.GetById<Provider>(id);
            genericRepository.Delete<Provider>(provider);
            genericRepository.SaveChanged();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            //db.Dispose();
            base.Dispose(disposing);
        }
    }
}